package com.love.marriage.eventRegistrationInfo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.love.marriage.common.interfaces.avoid.AvoidRepeatableCommit;
import com.love.marriage.common.interfaces.avoid.LogRestRequest;
import com.love.marriage.common.validator.Insert;
import com.love.marriage.common.validator.Update;
import com.love.marriage.common.vo.Result;
import com.love.marriage.common.vo.ResultBoolean;
import com.love.marriage.eventRegistrationInfo.service.EventRegistrationInfoService;
import com.love.marriage.eventRegistrationInfo.vo.EventRegistrationInfoVo;
import com.love.marriage.eventRegistrationInfo.vo.ResultEventRegistrationInfoVo;
import com.love.marriage.eventRegistrationInfo.vo.ResultEventRegistrationInfoVoPage;
import com.love.marriage.orderInfo.service.PayInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * description:  活动报名表restApi
 * author:       alex
 * createDate:   2024-10-15 04:29:12
 * version:      1.0.0
 */
@ApiSort(105)
@Tag(name = "活动报名表相关接口", description = "活动报名表相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/event-registration-info")
public class EventRegistrationInfoController {

    private final EventRegistrationInfoService eventRegistrationInfoService;

    private final PayInfoService payInfoService;

    @ApiOperationSupport(order = 10, author = "alex")
    @Operation(summary = "获取活动报名表分页", description = "获取活动报名表分页")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultEventRegistrationInfoVoPage.class
                    )))
    @PostMapping(value = "/page")
    @Parameters({
        @Parameter(name = "pageNum", description = "页码", required = false),
        @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    public Result<Page<EventRegistrationInfoVo>> getPage(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                             @RequestParam(value = "pageSize", required = false) Long pageSize,
                                             @RequestBody(required = false) EventRegistrationInfoVo eventRegistrationInfoVo) {
        return Result.success(eventRegistrationInfoService.getPage(pageNum, pageSize, eventRegistrationInfoVo));
    }

    @ApiOperationSupport(order = 20, author = "alex")
    @Operation(summary = "获取活动报名表详情", description = "获取活动报名表详情")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultEventRegistrationInfoVo.class
                    )))
    @Parameters({
        @Parameter(name = "id", description = "id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @GetMapping
    public Result<EventRegistrationInfoVo> query(@RequestParam(value = "id") Long id) {
        return Result.success(eventRegistrationInfoService.queryEventRegistrationInfo(id));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 30, author = "alex")
    @Operation(summary = "报名活动", description = "报名活动",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                        description = "查询条件",
                        required = false,
                        content = @Content(schema = @Schema(implementation = EventRegistrationInfoVo.class))
            ))
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultEventRegistrationInfoVo.class)))
    @PostMapping
    @Tag(name = "活动", description = "活动信息")
    public Result<EventRegistrationInfoVo> add(@Validated({Insert.class}) @RequestBody EventRegistrationInfoVo eventRegistrationInfoVo) throws Exception {
        return Result.success(eventRegistrationInfoService.addEventRegistrationInfo(eventRegistrationInfoVo));
    }

    @AvoidRepeatableCommit
    @LogRestRequest(apiName = "报名活动（微信支付）")
    @ApiOperationSupport(order = 30, author = "alex")
    @Operation(summary = "报名活动（微信支付）", description = "报名活动（微信支付）",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = EventRegistrationInfoVo.class))
            ))
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultEventRegistrationInfoVo.class)))
    @PostMapping(value = "event_wechat")
    @Tag(name = "活动", description = "活动信息")
    public Result<WxPayUnifiedOrderV3Result.JsapiResult> addWechat(@Validated({Insert.class}) @RequestBody EventRegistrationInfoVo eventRegistrationInfoVo) throws Exception {
        return Result.success(payInfoService.joinEventWechat(eventRegistrationInfoVo));
    }

    @ApiOperationSupport(order = 40, author = "alex")
    @Operation(summary = "修改活动报名表", description = "修改活动报名表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                        description = "查询条件",
                        required = false,
                        content = @Content(schema = @Schema(implementation = EventRegistrationInfoVo.class))
            ))
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultEventRegistrationInfoVo.class)))
    @PutMapping
    public Result<EventRegistrationInfoVo> update(@Validated({Update.class}) @RequestBody EventRegistrationInfoVo eventRegistrationInfoVo) {
        return Result.success(eventRegistrationInfoService.updateEventRegistrationInfo(eventRegistrationInfoVo));
    }

    @ApiOperationSupport(order = 50, author = "alex")
    @Operation(summary = "刪除活动报名表", description = "刪除活动报名表")
    @Parameters({
        @Parameter(name = "ids", description = "ids,用，分割", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1,2"))
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultBoolean.class)))
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam("ids") String ids) {
        return Result.success(eventRegistrationInfoService.deleteEventRegistrationInfo(ids));
    }

    @ApiOperationSupport(order = 60, author = "mybatisPlus")
    @Operation(summary = "导出活动名单", description = "导出活动名单")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultBoolean.class
                    )))
    @PostMapping(value = "/export")
    public void export(@RequestBody(required = false) EventRegistrationInfoVo eventRegistrationInfoVo, HttpServletResponse response) throws Exception {
        eventRegistrationInfoService.exportEventRegistrationInfo(eventRegistrationInfoVo, response);
    }
}
